# 编译器配置
CC = gcc
CFLAGS = -Wall -Wextra -pedantic -std=c99 -I.  # 修正为当前目录
LIB_SRC = rocerror.c      # 修正为当前目录
LIB_HDR = rocerror.h

# 目标定义
ORIGINALS = $(wildcard original/*.c)
MODIFIED = $(wildcard modified/*.c)  # 匹配所有 .c 文件（不带 _new）
BIN_ORIG = $(ORIGINALS:.c=)          # 原始目标：original/xxx.c → original/xxx
BIN_MOD = $(MODIFIED:.c=_new)        # 修改目标：modified/xxx.c → modified/xxx_new

# 默认目标
all: build_original build_modified

# 原始程序编译规则
build_original: $(BIN_ORIG)

original/%: original/%.c
	@echo "Compiling $<"
	$(CC) $(CFLAGS) $< -o $@

# 修改程序编译规则（修正文件名匹配逻辑）
build_modified: $(BIN_MOD)

# 规则：将 modified/xxx.c 编译为 modified/xxx_new
modified/%_new: modified/%.c $(LIB_SRC) $(LIB_HDR)
	@echo "Compiling $< and $(LIB_SRC)"
	$(CC) $(CFLAGS) $< $(LIB_SRC) -o $@

# 清理规则
clean:
	rm -f original/*~ modified/*~ $(BIN_ORIG) $(BIN_MOD)

# 测试规则（添加详细注释）
test: all
	@echo "\n========== Testing Original Programs =========="
	@echo "-- Testing original/01noerrmsg.c --"
	@./original/01noerrmsg || true
	@echo "-- Testing original/02rtnval.c --"
	@./original/02rtnval /root/testfile || true
	@echo "-- Testing original/03errno.c --"
	@./original/03errno /invalid/path || true
	@echo "-- Testing original/04merrno.c --"
	@./original/04merrno /invalid/path || true
	@echo "-- Testing original/05strerr.c --"
	@./original/05strerr /read/only/file || true
	@echo "-- Testing original/06perror.c --"
	@./original/06perror /read/only/file || true
	@echo "-- Testing original/07perror2.c --"
	@./original/07perror2 /read/only/file || true

	@echo "\n========== Testing Modified Programs =========="
	@echo "-- Testing modified/02rtnval_new.c --"
	@./modified/02rtnval_new /root/testfile || true
	@echo "-- Testing modified/03errno_new.c --"
	@./modified/03errno_new /invalid/path || true
	@echo "-- Testing modified/05strerr_new.c --"
	@./modified/05strerr_new /read/only/file || true

.PHONY: all clean test build_original build_modified